//
//  ImagePopView.m
//  QT
//
//  Created by 甘世清 on 2025/1/3.
//

#import "ImagePopView.h"

@implementation ImagePopView

- (void)SQ_UIViewForSelfView{
    self.page = 1;
    self.backgroundColor = UIColor.whiteColor;
    self.layer.cornerRadius = 30;
    self.layer.masksToBounds = YES;
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = SQColorF5F5F5;
    lineView.layer.cornerRadius = 4;
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
        make.top.equalTo(self).offset(10);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(8);
    }];
    [self addSubview:self.xiangjiBtn];
    [self addSubview:self.xiangceBtn];
    [self.xiangjiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(70);
        make.width.mas_equalTo(SCREEN_WIDETH);
        make.top.equalTo(lineView.mas_bottom).offset(20);
        make.centerX.equalTo(self);
    }];
    
    UIView * lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = SQColorF1F1F1;
    [self addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.xiangjiBtn.mas_bottom).offset(5);
        make.width.mas_equalTo(SCREEN_WIDETH);
        make.height.mas_equalTo(2);
    }];
    
    [self.xiangceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(70);
        make.width.mas_equalTo(SCREEN_WIDETH);
        make.top.equalTo(self.xiangjiBtn.mas_bottom).offset(10);
        make.centerX.equalTo(self);
    }];
    
    WS(weakSelf);
    [self.xiangjiBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_xiangjiBtn];
    }];
    [self.xiangceBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_xiangceBtn];
    }];
}
-(void)SQ_xiangjiBtn{
    WS(weakSelf);
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: sourceType]) {
        weakSelf.imagePickerVc.sourceType = sourceType;
        [[Manager SQ_getCurrentViewController] presentViewController:weakSelf.imagePickerVc animated:YES completion:nil];
    } else {
        NSLog(@"模拟器中无法打开照相机,请在真机中使用");
    }
}
-(void)SQ_xiangceBtn{
    WS(weakSelf);
    TZImagePickerController * vc = [[TZImagePickerController alloc]init];
    vc.maxImagesCount = self.page;
    [[Manager SQ_getCurrentViewController] presentViewController:vc animated:YES completion:nil];
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
         [weakSelf SQ_blick:photos];
    }];
}


- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *photo = [info objectForKey:UIImagePickerControllerEditedImage];
        if (photo == nil)
            {
                photo = [info objectForKey:UIImagePickerControllerOriginalImage];
            }
        [self SQ_blick:[[NSMutableArray alloc]initWithObjects:photo, nil]];
    }else
    {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"相册数据异常")];
    }
}
-(void)SQ_blick:(NSArray<UIImage *>*)imagearray{
    if (imagearray.count > 0) {
        if (self.addImageArrayBlock) {
            self.addImageArrayBlock(imagearray);
        }
    } else {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"数据异常")];
    }
    
}
- (SQCustomButton *)xiangjiBtn{
    if (!_xiangjiBtn) {
        _xiangjiBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _xiangjiBtn.text = SQNSLocalString(@"相机");
        _xiangjiBtn.font = FONT_MEDIUM(14);
        _xiangjiBtn.textColor = SQColor333333;
    }
    return _xiangjiBtn;
}
- (SQCustomButton *)xiangceBtn{
    if (!_xiangceBtn) {
        _xiangceBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _xiangceBtn.text = SQNSLocalString(@"相册");
        _xiangceBtn.font = FONT_MEDIUM(14);
        _xiangceBtn.textColor = SQColor333333;
    }
    return _xiangceBtn;
}
- (UIImagePickerController *)imagePickerVc{
    if (!_imagePickerVc) {
        _imagePickerVc = [[UIImagePickerController alloc] init];
        _imagePickerVc.delegate = self;
        _imagePickerVc.allowsEditing = NO;
    }
    return _imagePickerVc;
}

@end
